import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;
import exception.*;
public class Main {
    public String name1 = "Admin";
    public String password1 = "12345";
    public String name2 = "admin";
    public String password2 = "123456";

    public void login1(String name, String password)
            throws UserNameException,PassWordException{
        if(!this.name1.equals(name)) {
            System.out.println("用户名错误！");
            throw new UserNameException("你的用户名错了！");
        }
        if(!this.password1.equals(password)) {
            System.out.println("密码错误！");
            throw new PassWordException("你的密码错了！");
        }
    }
    public void login2(String name, String password)
            throws UserNameException,PassWordException{
        if(!this.name2.equals(name)) {
            System.out.println("用户名错误！");
            throw new UserNameException("你的用户名错了！");
        }
        if(!this.password2.equals(password)) {
            System.out.println("密码错误！");
            throw new PassWordException("你的密码错了！");
        }
    }

    public static User login() throws UserNameException,PassWordException{
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的姓名：");
        String name = scanner.nextLine();
        System.out.println("请输入你的密码：");
        String password = scanner.nextLine();
        Main a = new Main();
        System.out.println("请输入你的身份：1->管理员  0->普通用户");
        int choice = scanner.nextInt();
        if(choice == 1) {
                try {
                    a.login1(name, password);
                    return new AdminUser(name);
                } catch (UserNameException e) {
                    e.printStackTrace();
                    System.out.println("用户名异常！");
                } catch (PassWordException passWordException) {
                    passWordException.printStackTrace();
                    System.out.println("密码异常！");
                }

        }else {
                try {
                    a.login2(name, password);
                    return new NormalUser(name);
                } catch (UserNameException e) {
                    e.printStackTrace();
                    System.out.println("用户名异常！");
                } catch (PassWordException passWordException) {
                    passWordException.printStackTrace();
                    System.out.println("密码异常！");
                }
            }
        return null;
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = login();
        while(true) {
            int choice = user.menu();
            //根据 choice 和user 来确定 我到底 调用哪个对象的哪个操作
            user.doWork(choice, bookList);
        }

    }
}
